import { _decorator, Component, Node, Button, director } from 'cc';
import { GameManager } from '../core/GameManager';
import { PausePanel } from './PausePanel';
const { ccclass, property } = _decorator;

/**
 * 游戏界面UI控制脚本
 */
@ccclass('GameUI')
export class GameUI extends Component {
    @property(Button)
    private pauseButton: Button = null;
    
    @property(Button)
    private homeButton: Button = null;
    
    @property(Button)
    private restartButton: Button = null;
    
    @property(Node)
    private pausePanel: Node = null;
    
    @property(GameManager)
    private gameManager: GameManager = null;
    
    onLoad() {
        this.initUI();
        this.initButtonListeners();
        this.findGameManager();
    }
    
    onDestroy() {
        this.removeButtonListeners();
    }
    
    /**
     * 查找GameManager实例
     */
    private findGameManager(): void {
        if (!this.gameManager) {
            // 尝试在场景中查找GameManager
            const gameManagerNode = director.getScene().getChildByName('GameManager');
            if (gameManagerNode) {
                this.gameManager = gameManagerNode.getComponent(GameManager);
            }
            
            if (!this.gameManager) {
                console.warn('未找到GameManager引用，某些功能可能无法正常工作');
            }
        }
    }
    
    /**
     * 初始化UI状态
     */
    private initUI(): void {
        // 确保暂停面板初始隐藏
        if (this.pausePanel) {
            this.pausePanel.active = false;
        }
    }
    
    /**
     * 初始化按钮监听
     */
    private initButtonListeners(): void {
        if (this.pauseButton && this.pauseButton.node) {
            this.pauseButton.node.on(Button.EventType.CLICK, this.onPauseButtonClick, this);
        } else {
            console.warn('暂停按钮未设置或无效');
        }
        
        if (this.homeButton && this.homeButton.node) {
            this.homeButton.node.on(Button.EventType.CLICK, this.onHomeButtonClick, this);
        }
        
        if (this.restartButton && this.restartButton.node) {
            this.restartButton.node.on(Button.EventType.CLICK, this.onRestartButtonClick, this);
        }
        
        // 暂停面板上的按钮
        if (this.pausePanel) {
            const resumeBtn = this.pausePanel.getChildByName('ResumeButton');
            if (resumeBtn) {
                resumeBtn.on(Button.EventType.CLICK, this.onResumeButtonClick, this);
            }
            
            const menuBtn = this.pausePanel.getChildByName('MenuButton');
            if (menuBtn) {
                menuBtn.on(Button.EventType.CLICK, this.onHomeButtonClick, this);
            }
        }
    }
    
    /**
     * 移除按钮监听
     */
    private removeButtonListeners(): void {
        if (this.pauseButton && this.pauseButton.node) {
            this.pauseButton.node.off(Button.EventType.CLICK, this.onPauseButtonClick, this);
        }
        
        if (this.homeButton && this.homeButton.node) {
            this.homeButton.node.off(Button.EventType.CLICK, this.onHomeButtonClick, this);
        }
        
        if (this.restartButton && this.restartButton.node) {
            this.restartButton.node.off(Button.EventType.CLICK, this.onRestartButtonClick, this);
        }
        
        // 暂停面板上的按钮
        if (this.pausePanel) {
            const resumeBtn = this.pausePanel.getChildByName('ResumeButton');
            if (resumeBtn) {
                resumeBtn.off(Button.EventType.CLICK, this.onResumeButtonClick, this);
            }
            
            const menuBtn = this.pausePanel.getChildByName('MenuButton');
            if (menuBtn) {
                menuBtn.off(Button.EventType.CLICK, this.onHomeButtonClick, this);
            }
        }
    }
    
    /**
     * 暂停按钮点击处理
     */
    private onPauseButtonClick(): void {
        console.log("点击暂停按钮");
        this.showPausePanel();
    }
    
    /**
     * 显示暂停面板
     */
    public showPausePanel(): void {
        if (!this.pausePanel) {
            console.error('暂停面板未设置');
            return;
        }
        
        // 显示暂停面板
        this.pausePanel.active = true;
        
        // 获取暂停面板脚本并调用show方法
        const pausePanelComp = this.pausePanel.getComponent(PausePanel);
        if (pausePanelComp) {
            pausePanelComp.show();
        } else {
            console.warn('暂停面板缺少PausePanel组件');
            
            // 如果没有找到PausePanel组件，手动暂停游戏
            if (this.gameManager) {
                this.gameManager.pauseGame();
            }
        }
    }
    
    /**
     * 返回主菜单按钮点击处理
     */
    private onHomeButtonClick(): void {
        console.log("点击返回主菜单按钮");
        
        // 使用director直接加载主菜单
        director.loadScene('MainMenu', (err) => {
            if (err) {
                console.error('加载主菜单失败:', err);
            } else {
                console.log('成功加载主菜单');
            }
        });
    }
    
    /**
     * 重新开始按钮点击处理
     */
    private onRestartButtonClick(): void {
        console.log("点击重新开始按钮");
        
        // 重新开始游戏
        if (this.gameManager) {
            this.gameManager.restartGame();
        }
        
        // 隐藏暂停面板
        if (this.pausePanel && this.pausePanel.active) {
            this.pausePanel.active = false;
        }
    }
    
    /**
     * 继续游戏按钮点击处理
     */
    private onResumeButtonClick(): void {
        console.log("点击继续游戏按钮");
        
        // 隐藏暂停面板
        if (this.pausePanel) {
            this.pausePanel.active = false;
        }
        
        // 恢复游戏
        if (this.gameManager) {
            this.gameManager.resumeGame();
        }
    }
} 